import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @ClassName LC3
 * 无重复字符的最长子串
 * @Author ykx
 * @Date 2024/2/28 14:41
 * @Version 1.0
 */
public class LC3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 接收键盘上输入的字符串
        System.out.println("请输入字符串");
        String s = scanner.nextLine();
        System.out.println("最长的不重复字符串长度：" + lengthOfLongestSubstring(s));

    }

    public static int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        int ans = 0;
        int j = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i > 0) {
                set.remove(s.charAt(i - 1));
            }
            while (j < s.length() && !set.contains(s.charAt(j))) {
                set.add(s.charAt(j));
                j++;
            }
            ans = Math.max(ans, j - i);
        }
        return ans;
    }
}
